<template>
	<view>
		<!-- 逗留监测 -->
		<j-cell-group v-if="settingType == 1">
			<j-cell title="逗留侦测" :more="false">
				<view slot="tips" style="margin-right:20rpx;">
					<switch :checked="DoorConfig.PirConfig.Enable" color="#FF0025" style="transform:scale(0.8)"
						@change="PirConfigChange"></switch>
				</view>
			</j-cell>
			<!-- <template v-if="DoorConfig.PirConfig.Enable" v-for="(item, index) in Stay_detectionList">
				<j-cell :key="index" :title="item.text" :more="false" @jftap="jftapPirConfig(index,item.text)">
					<view slot="tips" v-if="item.value === DoorConfig.PirConfig.sensitivity">
						<uni-icons custom-prefix="iconfont" type="icon-duihao" size="24" color="#FF0025">
						</uni-icons>
					</view>
				</j-cell>
			</template> -->
			<j-cell-card v-if="DoorConfig.PirConfig.Enable" title="逗留时间(秒)" :show="true" :is-touch="false"
				:tips="interval" border style="background: #f5f7fb">
				<template>
					<view class="laba">
						<slider min="15" max="60" :show-value="true" style="width: 90%;"
							:value="DoorConfig.PirConfig.Pirtime" @change="jftapPirConfig" />
					</view>
				</template>
			</j-cell-card>
		</j-cell-group>
		<!-- 开锁方向设置 -->
		<j-cell-group v-if="settingType == 2">
			<template v-for="(item, index) in unlockingDirectionList">
				<j-cell :key="index" :title="item.text" :more="false" @jftap="jftapUnlockDirection(index,item.text)">
					<view slot="tips" v-if="item.value === DoorConfig.UnlockDirection.Direction">
						<uni-icons custom-prefix="iconfont" type="icon-duihao" size="24" color="#FF0025">
						</uni-icons>
					</view>
				</j-cell>
			</template>
		</j-cell-group>
		<!-- 自动反锁设置 -->
		<j-cell-group v-if="settingType == 3">
			<template v-for="(item, index) in automaticLockingList">
				<j-cell :key="index" :title="item.text" :more="false" @jftap="jftapAutoLock(index,item.text)">
					<view slot="tips" v-if="item.value === DoorConfig.AutoLock.LockMode">
						<uni-icons custom-prefix="iconfont" type="icon-duihao" size="24" color="#FF0025">
						</uni-icons>
					</view>
				</j-cell>
			</template>
			<j-cell-card v-if="DoorConfig.AutoLock.LockMode == 1" title="上锁时间(秒)" :show="true" :is-touch="false"
				:tips="interval" border style="background: #f5f7fb">
				<template>
					<view class="laba">
						<slider min="10" max="99" :show-value="true" style="width: 90%;"
							:value="DoorConfig.AutoLock.AutoLockInterval" @change="AutoLockChange" />
					</view>
				</template>
			</j-cell-card>
		</j-cell-group>
		<!-- 开锁模式设置 -->
		<j-cell-group v-if="settingType == 4">
			<template v-for="(item, index) in unlockingModeList">
				<j-cell :key="index" :title="item.text" :more="false" @jftap="jftapUnlockMode(index,item.text)">
					<view slot="tips" v-if="item.value === DoorConfig.UnlockMode.Mode">
						<uni-icons custom-prefix="iconfont" type="icon-duihao" size="24" color="#FF0025">
						</uni-icons>
					</view>
				</j-cell>
			</template>
		</j-cell-group>
		<!-- 音量设置 -->
		<view class="" v-if="settingType == 5">
			<j-cell-group>
				<j-cell title="音量设置" :more="false">
					<view slot="tips" style="margin-right:20rpx;">
						<switch :checked="volume_switch" color="#FF0025" style="transform:scale(0.8)"
							@change="volumeChange"></switch>
					</view>
				</j-cell>
			</j-cell-group>
			<view class="volume_hint">
				功能关闭后，你将无法听到智能锁的提示音。（包括按键，密码错误等提示）
			</view>
			<j-cell-group v-if="DoorConfig.VolumeControl.Volume != 0" title="选择锁音量">
				<template v-for="(item, index) in volumeList">
					<j-cell :key="index" :title="item.text" :more="false" @jftap="jftapvolume(index,item.text)">
						<view slot="tips" v-if="item.value === DoorConfig.VolumeControl.Volume">
							<uni-icons custom-prefix="iconfont" type="icon-duihao" size="24" color="#FF0025">
							</uni-icons>
						</view>
					</j-cell>
				</template>
			</j-cell-group>
		</view>
		<!-- 人脸验证提示音 -->
		<j-cell-group v-if="settingType == 6">
			<j-cell title="开启" :more="false">
				<view slot="tips" style="margin-right:20rpx;">
					<switch :checked="DoorConfig.FaceAlarmTone.Enable" color="#FF0025" style="transform:scale(0.8)"
						@change="FaceAlarmToneChange"></switch>
				</view>
			</j-cell>
		</j-cell-group>
		<!-- 电机扭矩调节 -->
		<j-cell-group v-if="settingType == 7">
			<template v-for="(item, index) in motortorqueRegulationList">
				<j-cell :key="index" :title="item.text" :more="false" @jftap="jftapMotorTorque(index,item.text)">
					<view slot="tips" v-if="item.value === DoorConfig.MotorTorque.Torque">
						<uni-icons custom-prefix="iconfont" type="icon-duihao" size="24" color="#FF0025">
						</uni-icons>
					</view>
				</j-cell>
			</template>
		</j-cell-group>
		<!-- 常开模式 -->
		<!-- DoorConfig.NormalOpenMode.Mode 关闭/开启(0/1) -->
		<j-cell-group v-if="settingType == 8">
			<j-cell title="开启" :more="false">
				<view slot="tips" style="margin-right:20rpx;">
					<switch :checked="normallyOpenMode" color="#FF0025" style="transform:scale(0.8)"
						@change="NormalOpenModeChange"></switch>
				</view>
			</j-cell>
		</j-cell-group>
		<!-- 防撬报警 -->
		<j-cell-group v-if="settingType == 9">
			<j-cell class="lineFeed" title="防撬报警" sub-title="开启后，门锁被撬动时会发出声光报警，同时联网上报云端" :more="false">
				<view slot="tips" style="margin-right:20rpx;">
					<switch :checked="Onekeydefense" color="#FF0025" style="transform:scale(0.8)"
						@change="DeployDefenseChange"></switch>
				</view>
			</j-cell>
		</j-cell-group>
		<!-- 人体感应 -->
		<view class="" v-if="settingType == 10">
			<j-cell-group>
				<j-cell title="人体感应" :more="false">
					<view slot="tips" style="margin-right:20rpx;">
						<switch :checked="DoorConfig.HumanSensor.Enable" color="#FF0025" style="transform:scale(0.8)"
							@change="HumanSensorChange"></switch>
					</view>
				</j-cell>
			</j-cell-group>
			<j-cell-group v-if="DoorConfig.HumanSensor.Enable" title="选择距离">
				<template v-for="(item, index) in HumanSensorList">
					<j-cell :key="index" :title="item.text" :more="false" @jftap="jftapHumanSensor(index,item.text)">
						<view slot="tips" v-if="item.value === DoorConfig.HumanSensor.sensitivity">
							<uni-icons custom-prefix="iconfont" type="icon-duihao" size="24" color="#FF0025">
							</uni-icons>
						</view>
					</j-cell>
				</template>
			</j-cell-group>
		</view>

		<j-cell-group v-if="installSetList.length > 0">
			<template v-for="(item,index) in installSetList">
				<j-cell :key="index" v-if="item.status" :title="item.name" :to="'/jvss/pagesDevice/device/device-live/door-lock/setting/lockSetting/functionPrompt?token=' 
					+ device.token + '&title=' +item.name + '&type=' +item.type+ '&function=' +item.function">
				</j-cell>
			</template>
		</j-cell-group>

		<view class="foot-button">
			<button class="block radius-btn"
				v-if="settingType == 1 || settingType == 3 || settingType == 5 || settingType == 10"
				@tap="saveSettings()" type="operation">保存</button>
		</view>
	</view>
</template>

<script>
	const {
		turnTenToSixteen
	} = require('@/utils/util.js');
	const {
		handledoorLockTransparent
	} = require('@/jvss/pagesDevice/utils/index.js');
	export default {
		data() {
			return {
				Stay_detection: false,
				// 逗留集合
				Stay_detectionList: [{
						text: '低',
						value: 0
					},
					{
						text: '中',
						value: 1
					},
					{
						text: '高',
						value: 2
					},
				],
				// 开锁方向
				unlockingDirectionList: [{
						text: '向左开',
						value: 0
					},
					{
						text: '向右开',
						value: 1
					},
				],
				// 自动反锁设置
				automaticLockingList: [{
						text: '手动',
						value: 0
					},
					{
						text: '自动',
						value: 1
					},
				],
				// 上锁时间
				volume: 85,
				// 开锁模式
				unlockingModeList: [{
						text: '单开模式',
						value: 0
					},
					{
						text: '组合模式 ',
						value: 1
					},
				],
				// 音量设置
				volume_switch: false,
				volumeList: [{
						text: '低',
						value: 1
					},
					{
						text: '中',
						value: 2
					},
					{
						text: '高',
						value: 3
					},
				],
				// 人体感应
				HumanSensor_switch: false,
				HumanSensorList: [{
						text: '短距',
						value: 0
					},
					{
						text: '中距',
						value: 1
					},
					{
						text: '长距',
						value: 2
					},
				],
				// 人脸验证提示音
				faceVerification: false,
				// 电机扭矩调节
				motortorqueRegulationList: [{
						text: '小',
						value: 0
					},
					{
						text: '中',
						value: 1
					},
					{
						text: '大',
						value: 2
					},
				],
				// 常开模式
				normallyOpenMode: false,
				// 一键布防
				Onekeydefense: false,
				settingType: null,
				DoorConfig: null,
				device: null,
				installSetList: []
			}
		},
		onLoad(opt) {
			this.device = opt
			this.settingType = opt.type;
			this.installSetList = opt.installSetList ? JSON.parse(opt.installSetList) : [];
			if (this.installSetList.length == 0) {
				this.doorLockTransparent()
			}
			uni.setNavigationBarTitle({
				title: opt.title,
			});
		},
		methods: {
			doorLockTransparent() {
				uni.showLoading({
					title: '加载中...',
					mask: true
				});
				let data = {
					Name: 'OPDoorLockProCmd',
					OPDoorLockProCmd: {
						Cmd: "GetDoorConfig"
					}
				};
				let token = this.device.token;
				console.log(this.DoorConfig)
				getApp().globalData.api.doorLockTransparent(data, token, (result) => {
					uni.hideLoading()
					console.log('门锁获取设置：', result);
					handledoorLockTransparent(result, (res, Err) => {
						console.log(res, 'ress')
						let DoorConfig = res
						this.DoorConfig = DoorConfig ? DoorConfig : null
						this.volume_switch = this.settingType == 5 ? DoorConfig.VolumeControl.Volume == 0 ?
							false :
							true : false
						this.normallyOpenMode = this.settingType == 8 ? DoorConfig.NormalOpenMode.Mode ==
							0 ?
							false :
							true : false
						this.Onekeydefense = this.settingType == 9 ? DoorConfig.DeployDefense.Enable ==
							'00' ?
							false :
							true : false
					}, false, () => {}, 'DoorConfig')
				}, false);
			},
			// 逗留侦测开关
			PirConfigChange(e) {
				this.DoorConfig.PirConfig.Enable = e.detail.value
			},
			// 逗留侦测
			jftapPirConfig(e) {
				this.DoorConfig.PirConfig.Pirtime = e.detail.value
				// this.$delete(this.DoorConfig.PirConfig, 'sensitivity')
			},
			// 开锁方向
			jftapUnlockDirection(index) {
				console.log(index)
				this.DoorConfig.UnlockDirection.Direction = index;
				console.log(this.DoorConfig.UnlockDirection.Direction, '开锁方向')
				this.save('UnlockDirection', this.DoorConfig.UnlockDirection)
			},
			// 自动反锁
			jftapAutoLock(index) {
				this.DoorConfig.AutoLock.LockMode = index
			},
			AutoLockChange(e) {
				this.DoorConfig.AutoLock.AutoLockInterval = e.detail.value
			},
			// 开锁模式
			jftapUnlockMode(index) {
				console.log(index, '开锁模式')
				let UnlockMode = this.DoorConfig.UnlockMode
				this.DoorConfig.UnlockMode.Mode = index;
				this.save('UnlockMode', this.DoorConfig.UnlockMode);
			},
			// 音量设置
			volumeChange(e) {
				if (e.detail.value == false) {
					this.DoorConfig.VolumeControl.Volume = 0
				} else {
					this.DoorConfig.VolumeControl.Volume = 1
				}
			},
			// 音量设置大小
			jftapvolume(index) {
				this.DoorConfig.VolumeControl.Volume = index + 1
			},
			// 人体感应
			HumanSensorChange(e) {
				this.DoorConfig.HumanSensor.Enable = e.detail.value;
				console.log(e.detail.value)
			},
			jftapHumanSensor(index) {
				this.DoorConfig.HumanSensor.sensitivity = index

			},
			// 人脸提示音
			FaceAlarmToneChange(e) {
				this.DoorConfig.FaceAlarmTone.Enable = e.detail.value;
				this.save('FaceAlarmTone', this.DoorConfig.FaceAlarmTone)
			},
			// 电机扭矩调节
			jftapMotorTorque(index) {
				this.DoorConfig.MotorTorque.Torque = index
				this.save('MotorTorque', this.DoorConfig.MotorTorque)
			},
			// 常开模式
			NormalOpenModeChange(e) {
				if (e.detail.value == false) {
					this.DoorConfig.NormalOpenMode.Mode = 0
				} else {
					this.DoorConfig.NormalOpenMode.Mode = 1
				}
				this.save('NormalOpenMode', this.DoorConfig.NormalOpenMode)
			},
			// 防撬报警
			DeployDefenseChange(e) {
				if (e.detail.value == false) {
					this.DoorConfig.DeployDefense.Enable = '00'
				} else {
					this.DoorConfig.DeployDefense.Enable = 1
				}
				this.save('DeployDefense', this.DoorConfig.DeployDefense)
			},
			// 保存按钮
			saveSettings() {
				if (this.settingType == 1) {
					this.save('PirConfig', this.DoorConfig.PirConfig)
				}
				if (this.settingType == 3) {
					this.save('AutoLock', this.DoorConfig.AutoLock)
				}
				if (this.settingType == 5) {
					this.save('VolumeControl', this.DoorConfig.VolumeControl)
				}
				if (this.settingType == 10) {
					this.save('HumanSensor', this.DoorConfig.HumanSensor)
				}
			},
			// 设置
			save(type, value) {
				console.log(type, value)
				let token = this.device.token;
				let data = {
					Name: "OPDoorLockProCmd",
					OPDoorLockProCmd: {
						Cmd: "SetDoorConfig",
						DoorConfig: {}
					}
				};
				data.OPDoorLockProCmd.DoorConfig[type] = value
				console.log(data)
				uni.showLoading({
					title: '设置中...'
				});
				getApp().globalData.api.doorLockTransparent(data, token, (result) => {
					uni.hideLoading()
					handledoorLockTransparent(result, (res, Err) => {
						if (Err === '0x00') {
							uni.showToast({
								title: '保存成功'
							})
						} else {
							uni.showToast({
								icon: 'error',
								title: '保存失败'
							})
						}
					}, true, () => {}, 'SetDoorConfig')
					console.log('门锁设置：', result);
				}, false);
			}
		}
	}
</script>

<style lang="scss">
	.bottm {
		position: fixed;
		left: 30rpx;
		right: 30rpx;
		bottom: 40rpx;
		height: 84rpx;
		background: fff;
		border: 1rpx solid #B9B9B9;
		border-radius: 15rpx;
		ont-size: 30rpx;
		color: #FF0025;
		font-weight: 700;
	}

	.volume_hint {
		text-align: center;
		color: #999;
		padding: 40rpx;

	}

	.lineFeed {
		/deep/.cell-sub-title {
			word-wrap: break-word;
			/*允许长单词换行到下一行*/
			word-break: break-all;
			/*这个参数根据需要来决定要不要*/
			overflow: hidden;
			/*这个参数根据需要来决定要不要*/
			white-space: break-spaces;
			text-align: start;
		}
	}
</style>